package cn.kgc.util;

import cn.kgc.common.dtoutils.dtoUtil;
import cn.kgc.dtos.Dto;
import cn.kgc.vo.ItripHotelVo;
import cn.kgc.vo.ReturnSearchHotel;
import cn.kgc.voi.HotCityVo;
import cn.kgc.voi.SearchQueryVo;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

public class SolrjUtil {
    @Resource
    private HttpSolrClient httpSolrClient;
    private QueryResponse queryResponse = null;

     public SolrjUtil(HttpSolrClient httpSolrClient) {
        this.httpSolrClient =httpSolrClient;
    }

    public Dto search(SearchQueryVo sqv) {
        SolrjUtil solrjUtil = new SolrjUtil(httpSolrClient);
        //创建查询对象
        SolrQuery solrQuery = new SolrQuery();
        //关键词
        solrQuery.setQuery("*:*");
        if (sqv != null) {
            //关键字
            if (null !=sqv.getKeywords() && "" != sqv.getKeywords()) {
                solrQuery.addFilterQuery("keywords:"+sqv.getKeywords());
            }
            //酒店等级
            if (sqv.getHotelLevel()  != 0) {
                solrQuery.addFilterQuery("hotelLevel:"+sqv.getHotelLevel());
            }
            //商圈
            if (sqv.getTradeAreaIds() != null && "" != sqv.getTradeAreaIds()) {
                solrQuery.addFilterQuery("tradingAreaIds:," + sqv.getTradeAreaIds()+",");
            }
            //酒店特色
            if (sqv.getFeatureIds() != null && "" != sqv.getFeatureIds()) {
                solrQuery.addFilterQuery("featureIds:*,"+sqv.getFeatureIds()+",*");
            }
            //最高价 //最低价
            if (sqv.getMaxPrice() != null && "" != sqv.getMaxPrice() && sqv.getMinPrice() != null && "" != sqv.getMinPrice()) {
                solrQuery.addFilterQuery("minPrice:[" + sqv.getMinPrice()+" TO "+sqv.getMaxPrice()+"]");
            }
            //目的地
            if (sqv.getDestination() != null && "" != sqv.getDestination()) {
                solrQuery.addFilterQuery("destination:"+sqv.getDestination());
            }
            //升序
            if (sqv.getAscSort() != null && "" != sqv.getAscSort()) {
                solrQuery.addSort(SolrQuery.SortClause.create(sqv.getAscSort(), SolrQuery.ORDER.asc));
            }
            //降序
            if (sqv.getDescSort() != null && "" != sqv.getDescSort()) {
                solrQuery.addSort(SolrQuery.SortClause.create(sqv.getDescSort(), SolrQuery.ORDER.desc));
            }
        }
        solrQuery.setRows(64);
        //执行查询
        List<ReturnSearchHotel> hotelList = null;
        hotelList = solrjUtil.query(solrQuery);
        long count = hotelList.size();
        long pageno = 1;
        if (null != sqv.getPageNo()) {
            pageno = Long.parseLong(sqv.getPageNo());
        }
        int start = 0;
        if (sqv.getPageNo() != null && "" != sqv.getPageNo()) {
            int num = Integer.parseInt(sqv.getPageNo()) - 1;
            start = num * 5;
        }
        solrQuery.setStart(start);
        int rows = 5;
        if (sqv.getPageSize() != null && "" != sqv.getPageSize()) {
            rows = 5 * Integer.parseInt(sqv.getPageNo());
        }
        solrQuery.setRows(rows);

        hotelList = solrjUtil.query(solrQuery);


        ItripHotelVo itripHotelVo = new ItripHotelVo();
        itripHotelVo.setRows(hotelList);
        itripHotelVo.setTotal(count);
        long pageCount = count % 5 == 0 ? count / 5 : count / 5 + 1;
        itripHotelVo.setBeginPos((pageno - 1) * 5);
        itripHotelVo.setPageCount(pageCount);
        itripHotelVo.setCurPage(pageno);
        itripHotelVo.setPageSize(5);
        return dtoUtil.getDto("", "", itripHotelVo, "true", null);
    }

    public List<ReturnSearchHotel> query(SolrQuery solrQuery) {
        List<ReturnSearchHotel> hotelList = null;
        try {
            queryResponse = httpSolrClient.query(solrQuery);
            hotelList = queryResponse.getBeans(ReturnSearchHotel.class);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return hotelList;
    }

    public Dto search(HotCityVo hotCityVo){
        //创建查询对象
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");//关键词
        if (hotCityVo.getCount()!=null){//页大小
            solrQuery.setStart(0);
            solrQuery.setRows(Integer.parseInt(hotCityVo.getCount()));
        }
        if (hotCityVo.getCityId()!=null){//id
            solrQuery.setQuery("cityId:"+hotCityVo.getCityId());
        }
        //执行查询
        List<ReturnSearchHotel> hotelList = null;
        try {
            queryResponse = httpSolrClient.query(solrQuery);
            hotelList = queryResponse.getBeans(ReturnSearchHotel.class);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dtoUtil.getDto("","",hotelList,"true",null);

    }

}
